function isTextDecoration(st, val){
	if(val.indexOf(st) != -1){
		return true;	
	}
	return false;
}

//checks the states of the button, on or off
function checkButtonState(){
	for(p in butter.buttonState){					
			if(window.getSelection().rangeCount){
			
				var range = window.getSelection().getRangeAt(0);
				
				var styleName = butter.buttonConvert[p];
				var styleValue = butter.buttonMatchOn[p];
				
				theswitch = 0;
				
				// if it is an empty range and single caret
				if(range == ''){
					
					
					$(range.startContainer).parents().each(function(){							
						
						if(styleName == "textDecoration"){
							if(isTextDecoration(styleValue ,this.style[styleName])){
								theswitch = 1;
								return false;
							}
							
						} else {
						
							if(member1(styleName, this.style[styleName], butter.buttonOnStates[p])){						
								theswitch = 1;	
								return false;
							}
						
						}
						/*else if(member1(styleName, this.style[styleName], butter.buttonOffStates[p])){
							
							
							theswitch = 0;
							return false;
						}
						*/
						
					});
				}  else {     // if it is an selected range of text
					frontFlag = 0;
					backFlag = 0;
					existMiddle = 0;
					middleFlag = 1;
					
					$(range.startContainer).parents().each(function(){
						if(styleName == "textDecoration"){
							if(isTextDecoration(styleValue ,this.style[styleName])){
								frontFlag = 1;
								return false;
							} else {
								frontFlag = 0;
								return false;	
							}
							
						} else {
						
							if(member1(styleName, this.style[styleName], butter.buttonOnStates[p])){
								frontFlag = 1;
								
								return false;
							} else if(member1(styleName, this.style[styleName], butter.buttonOffStates[p])){
								frontFlag = 0;
								return false;
							}
						
						}
						 
					});	
				
					
					cloneContents = range.cloneContents();
					
					for(i=0; i<cloneContents.childNodes.length; i++){
						if(cloneContents.childNodes[i].nodeType == 1){
							existMiddle = 1;
												/*if(member1(cloneContents.childNodes[i].style[styleName],butter.buttonOffStates[p])){
								middleFlag = 0;								
								
								if(spanParentsStyle(range, p)){
									middleFlag = 1;	
								}
							
							}*/
						}
					}
											
					$(range.endContainer).parents().each(function(){
						
						if(styleName == "textDecoration"){
							if(isTextDecoration(styleValue ,this.style[styleName])){
								backFlag = 1;
								return false;
							}
							
						} else {
						
							if(member1(styleName, this.style[styleName], butter.buttonOnStates[p])){
								backFlag = 1;
								
								return false;
							} else if(member1(styleName, this.style[styleName], butter.buttonOffStates[p])){
								backFlag = 0;
								return false;
							}
						
						}
					});		
					
						
					
					if(frontFlag && backFlag ){ //&& (!existMiddle || middleFlag)
						theswitch = 1;	
					}
					
				} 
						
				if(theswitch == 0){
					$("#" + p).find("img").css({
						"border-top": "none",
						"border-right": "none"
					})
				} else if (theswitch ==1){
					$("#" + p).find("img").css({
						"border-top": "1px solid black",
						"border-right": "1px solid black"
					})				
				}
			}
			
			if(theswitch == 0){
				butter.buttonState[p] = 0;	
			} else {
				butter.buttonState[p] = 1;	
			}
			
		}
}